from typing import List


def fourSum(nums: List[int], target: int):
    """
    比三数之和多一层循环
    :param nums:
    :param target:
    :return:
    """
    result, n = [], len(nums)
    if n < 4:
        return []
    nums.sort()
    for i in range(n - 3):
        first = nums[i]
        for j in range(i + 1, n - 2):
            second = nums[j]
            left, right = j + 1, n - 1
            while left < right:
                sums = first + second + nums[left] + nums[right]
                if sums == target:
                    tmp = [first, second, nums[left], nums[right]]
                    if tmp not in result:
                        result.append(tmp)
                    left += 1
                    right -= 1
                elif sums > target:
                    right -= 1
                else:
                    left += 1
    return result


if __name__ == '__main__':
    print(fourSum([1, 0, -1, 0, -2, 2], 0))
